package biz.obake.team.touchprotector.lfd;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import biz.obake.team.android.a;
import biz.obake.team.android.e;
import biz.obake.team.touchprotector.R;
import biz.obake.team.touchprotector.c;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;

public class b implements a {
    private static b n;

    /* renamed from: a  reason: collision with root package name */
    private ArrayList<String> f1729a;

    /* renamed from: b  reason: collision with root package name */
    private String f1730b;

    /* renamed from: c  reason: collision with root package name */
    private int f1731c;

    /* renamed from: d  reason: collision with root package name */
    private int f1732d;
    private int e;
    private int f;
    private String g;
    private String h;
    private String i;
    private String j;
    private int k;
    private int l;
    private int m;

    private b() {
        k();
    }

    private int e() {
        Resources resources = a.a().getResources();
        this.k = resources.getDisplayMetrics().widthPixels;
        int i2 = resources.getDisplayMetrics().heightPixels;
        this.l = i2;
        this.m = Math.max(this.k, i2);
        int i3 = resources.getDisplayMetrics().densityDpi;
        if (i3 > 320) {
            this.m = (this.m * 320) / i3;
        }
        int highestOneBit = Integer.highestOneBit(this.f / this.m);
        if (highestOneBit == 0) {
            return 1;
        }
        return highestOneBit;
    }

    private void h() {
        String str;
        int i2;
        String absolutePath = a.a().getFileStreamPath("lock_frame_image_tmp").getAbsolutePath();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(absolutePath, options);
        int i3 = options.outWidth;
        this.f1732d = i3;
        int i4 = options.outHeight;
        this.e = i4;
        this.f = Math.min(i3, i4);
        String str2 = options.outMimeType;
        this.f1730b = str2;
        if ("image/jpeg".equals(str2)) {
            str = "JPEG";
        } else if ("image/png".equals(this.f1730b)) {
            str = l() ? "PNG_9PATCH" : "PNG";
        } else {
            throw new Exception(c.w(R.string.lfd_custom_err_wrong_type));
        }
        this.g = str;
        this.f1731c = 0;
        if ("image/jpeg".equals(this.f1730b)) {
            int attributeInt = new ExifInterface(absolutePath).getAttributeInt("Orientation", 1);
            if (attributeInt == 3) {
                i2 = 180;
            } else if (attributeInt == 6) {
                i2 = 90;
            } else if (attributeInt == 8) {
                i2 = 270;
            } else {
                return;
            }
            this.f1731c = i2;
        }
    }

    public static b i() {
        if (n == null) {
            n = new b();
        }
        return n;
    }

    private void k() {
        this.f1729a = new ArrayList<>();
        for (String str : a.a().fileList()) {
            if (str.startsWith("lock_frame_info_")) {
                this.f1729a.add(str.substring(16));
            }
        }
        Collections.sort(this.f1729a);
        Collections.reverse(this.f1729a);
    }

    private boolean l() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = a.a().openFileInput("lock_frame_image_tmp");
            new e(BitmapFactory.decodeStream(fileInputStream));
            if (fileInputStream == null) {
                return true;
            }
            biz.obake.team.android.c.a(fileInputStream);
            return true;
        } catch (Exception unused) {
            if (fileInputStream != null) {
                biz.obake.team.android.c.a(fileInputStream);
            }
            return false;
        } catch (Throwable th) {
            if (fileInputStream != null) {
                biz.obake.team.android.c.a(fileInputStream);
            }
            throw th;
        }
    }

    /* JADX WARNING: type inference failed for: r0v0, types: [android.graphics.Rect, java.io.Closeable] */
    private void n(InputStream inputStream, int i2) {
        boolean z;
        FileOutputStream fileOutputStream = 0;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = i2;
            Bitmap decodeStream = BitmapFactory.decodeStream(inputStream, fileOutputStream, options);
            if (decodeStream != null) {
                this.f1732d = decodeStream.getWidth();
                int height = decodeStream.getHeight();
                this.e = height;
                this.f = Math.min(this.f1732d, height);
                Matrix matrix = new Matrix();
                boolean z2 = true;
                if (this.f > this.m) {
                    float f2 = ((float) this.m) / ((float) this.f);
                    matrix.postScale(f2, f2);
                    z = true;
                } else {
                    z = false;
                }
                if (this.f1731c > 0) {
                    matrix.postRotate((float) this.f1731c);
                } else {
                    z2 = z;
                }
                if (z2) {
                    Bitmap createBitmap = Bitmap.createBitmap(decodeStream, 0, 0, this.f1732d, this.e, matrix, true);
                    if (createBitmap != null) {
                        decodeStream.recycle();
                        decodeStream = createBitmap;
                    } else {
                        throw new IOException("Can't rotate bitmap.");
                    }
                }
                Bitmap.CompressFormat compressFormat = "image/jpeg".equals(this.f1730b) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG;
                fileOutputStream = a.a().openFileOutput(this.i, 0);
                decodeStream.compress(compressFormat, 100, fileOutputStream);
                return;
            }
            throw new IOException("Can't decode bitmap.");
        } finally {
            biz.obake.team.android.c.a(fileOutputStream);
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x003c A[ADDED_TO_REGION] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void o() {
        /*
            r5 = this;
            java.lang.String r0 = r5.g
            int r1 = r0.hashCode()
            r2 = 79369(0x13609, float:1.1122E-40)
            r3 = 2
            r4 = 1
            if (r1 == r2) goto L_0x002c
            r2 = 2283624(0x22d868, float:3.200039E-39)
            if (r1 == r2) goto L_0x0022
            r2 = 1518009989(0x5a7afe85, float:1.76621478E16)
            if (r1 == r2) goto L_0x0018
            goto L_0x0036
        L_0x0018:
            java.lang.String r1 = "PNG_9PATCH"
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L_0x0036
            r0 = 2
            goto L_0x0037
        L_0x0022:
            java.lang.String r1 = "JPEG"
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L_0x0036
            r0 = 0
            goto L_0x0037
        L_0x002c:
            java.lang.String r1 = "PNG"
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L_0x0036
            r0 = 1
            goto L_0x0037
        L_0x0036:
            r0 = -1
        L_0x0037:
            java.lang.String r1 = "lock_frame_image_tmp"
            r2 = 0
            if (r0 == 0) goto L_0x0057
            if (r0 == r4) goto L_0x0057
            if (r0 == r3) goto L_0x0041
            goto L_0x0069
        L_0x0041:
            biz.obake.team.android.a r0 = biz.obake.team.android.a.a()     // Catch:{ all -> 0x0052 }
            java.io.FileInputStream r2 = r0.openFileInput(r1)     // Catch:{ all -> 0x0052 }
            java.lang.String r0 = r5.i     // Catch:{ all -> 0x0052 }
            biz.obake.team.android.c.d(r2, r0)     // Catch:{ all -> 0x0052 }
            biz.obake.team.android.c.a(r2)
            goto L_0x0069
        L_0x0052:
            r0 = move-exception
            biz.obake.team.android.c.a(r2)
            throw r0
        L_0x0057:
            int r0 = r5.e()
        L_0x005b:
            biz.obake.team.android.a r3 = biz.obake.team.android.a.a()     // Catch:{ OutOfMemoryError -> 0x006e, all -> 0x006c }
            java.io.FileInputStream r3 = r3.openFileInput(r1)     // Catch:{ OutOfMemoryError -> 0x006e, all -> 0x006c }
            r5.n(r3, r0)     // Catch:{ OutOfMemoryError -> 0x006a }
            biz.obake.team.android.c.a(r3)
        L_0x0069:
            return
        L_0x006a:
            goto L_0x006f
        L_0x006c:
            r0 = move-exception
            goto L_0x0088
        L_0x006e:
            r3 = r2
        L_0x006f:
            int r0 = r0 << 1
            r4 = 16
            if (r0 > r4) goto L_0x0079
            biz.obake.team.android.c.a(r3)
            goto L_0x005b
        L_0x0079:
            java.lang.Exception r0 = new java.lang.Exception     // Catch:{ all -> 0x0086 }
            r1 = 2131624036(0x7f0e0064, float:1.887524E38)
            java.lang.String r1 = biz.obake.team.touchprotector.c.w(r1)     // Catch:{ all -> 0x0086 }
            r0.<init>(r1)     // Catch:{ all -> 0x0086 }
            throw r0     // Catch:{ all -> 0x0086 }
        L_0x0086:
            r0 = move-exception
            r2 = r3
        L_0x0088:
            biz.obake.team.android.c.a(r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: biz.obake.team.touchprotector.lfd.b.o():void");
    }

    private void p() {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("title", "");
        jSONObject.put("type", this.g);
        biz.obake.team.android.c.e(jSONObject.toString(), this.j);
    }

    private void q(Uri uri) {
        InputStream inputStream = null;
        try {
            inputStream = a.a().getContentResolver().openInputStream(uri);
            biz.obake.team.android.c.d(inputStream, "lock_frame_image_tmp");
        } finally {
            biz.obake.team.android.c.a(inputStream);
        }
    }

    public String a(int i2) {
        return String.format("custom:%s", new Object[]{this.f1729a.get(i2)});
    }

    /* JADX WARNING: type inference failed for: r0v0 */
    /* JADX WARNING: type inference failed for: r0v1, types: [android.graphics.drawable.Drawable] */
    /* JADX WARNING: type inference failed for: r0v2, types: [java.io.Closeable] */
    /* JADX WARNING: type inference failed for: r0v3 */
    /* JADX WARNING: type inference failed for: r0v4 */
    /* JADX WARNING: type inference failed for: r0v5 */
    /* JADX WARNING: Multi-variable type inference failed */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public android.graphics.drawable.Drawable b(int r9) {
        /*
            r8 = this;
            r0 = 0
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            r1.<init>()     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.lang.String r2 = "lock_frame_image_"
            r1.append(r2)     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.util.ArrayList<java.lang.String> r2 = r8.f1729a     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.lang.Object r2 = r2.get(r9)     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.lang.String r2 = (java.lang.String) r2     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            r1.append(r2)     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.lang.String r1 = r1.toString()     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.lang.String r9 = r8.j(r9)     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            biz.obake.team.android.a r2 = biz.obake.team.android.a.a()     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            android.content.res.Resources r2 = r2.getResources()     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            biz.obake.team.android.a r3 = biz.obake.team.android.a.a()     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            java.io.FileInputStream r1 = r3.openFileInput(r1)     // Catch:{ Exception -> 0x009e, all -> 0x0099 }
            r3 = -1
            int r4 = r9.hashCode()     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r5 = 79369(0x13609, float:1.1122E-40)
            r6 = 2
            r7 = 1
            if (r4 == r5) goto L_0x0059
            r5 = 2283624(0x22d868, float:3.200039E-39)
            if (r4 == r5) goto L_0x004f
            r5 = 1518009989(0x5a7afe85, float:1.76621478E16)
            if (r4 == r5) goto L_0x0045
            goto L_0x0062
        L_0x0045:
            java.lang.String r4 = "PNG_9PATCH"
            boolean r9 = r9.equals(r4)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            if (r9 == 0) goto L_0x0062
            r3 = 0
            goto L_0x0062
        L_0x004f:
            java.lang.String r4 = "JPEG"
            boolean r9 = r9.equals(r4)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            if (r9 == 0) goto L_0x0062
            r3 = 2
            goto L_0x0062
        L_0x0059:
            java.lang.String r4 = "PNG"
            boolean r9 = r9.equals(r4)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            if (r9 == 0) goto L_0x0062
            r3 = 1
        L_0x0062:
            if (r3 == 0) goto L_0x0074
            if (r3 == r7) goto L_0x0069
            if (r3 == r6) goto L_0x0069
            goto L_0x009f
        L_0x0069:
            android.graphics.Bitmap r9 = android.graphics.BitmapFactory.decodeStream(r1)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            android.graphics.drawable.BitmapDrawable r3 = new android.graphics.drawable.BitmapDrawable     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r3.<init>(r2, r9)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r0 = r3
            goto L_0x009f
        L_0x0074:
            biz.obake.team.android.e r9 = new biz.obake.team.android.e     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            android.graphics.Bitmap r3 = android.graphics.BitmapFactory.decodeStream(r1)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r9.<init>(r3)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r3 = 320(0x140, float:4.48E-43)
            r9.n(r3)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            android.graphics.NinePatch r3 = new android.graphics.NinePatch     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            android.graphics.Bitmap r4 = r9.d()     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            byte[] r9 = r9.c()     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r3.<init>(r4, r9, r0)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            android.graphics.drawable.NinePatchDrawable r9 = new android.graphics.drawable.NinePatchDrawable     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r9.<init>(r2, r3)     // Catch:{ Exception -> 0x009f, all -> 0x0096 }
            r0 = r9
            goto L_0x009f
        L_0x0096:
            r9 = move-exception
            r0 = r1
            goto L_0x009a
        L_0x0099:
            r9 = move-exception
        L_0x009a:
            biz.obake.team.android.c.a(r0)
            throw r9
        L_0x009e:
            r1 = r0
        L_0x009f:
            biz.obake.team.android.c.a(r1)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: biz.obake.team.touchprotector.lfd.b.b(int):android.graphics.drawable.Drawable");
    }

    public String c(int i2) {
        String str;
        try {
            str = new JSONObject(biz.obake.team.android.c.c("lock_frame_info_" + this.f1729a.get(i2))).getString("title");
        } catch (Exception unused) {
            str = "";
        }
        return "".equals(str) ? c.w(R.string.lfd_custom_title_hint) : str;
    }

    public void d(Uri uri) {
        this.h = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        this.j = "lock_frame_info_" + this.h;
        this.i = "lock_frame_image_" + this.h;
        try {
            q(uri);
            h();
            o();
            p();
            k();
        } catch (Throwable th) {
            a.a().deleteFile(this.j);
            a.a().deleteFile(this.i);
            throw th;
        }
    }

    public void f(int i2) {
        String str = "lock_frame_image_" + this.f1729a.get(i2);
        a.a().deleteFile(str);
        a.a().deleteFile("lock_frame_info_" + this.f1729a.get(i2));
        k();
        f.d().b();
    }

    public int g(String str) {
        return this.f1729a.indexOf(str.substring(7));
    }

    public int getCount() {
        return this.f1729a.size();
    }

    public String j(int i2) {
        String str;
        try {
            str = new JSONObject(biz.obake.team.android.c.c("lock_frame_info_" + this.f1729a.get(i2))).getString("type");
        } catch (IOException | JSONException unused) {
            str = "";
        }
        if (!"".equals(str)) {
            return str;
        }
        throw new biz.obake.team.touchprotector.g.b("{0C738814-C8F3-44DD-B573-683C54CCC6EE}", "id:%d", Integer.valueOf(i2));
    }

    public void m(int i2, String str) {
        try {
            String str2 = "lock_frame_info_" + this.f1729a.get(i2);
            JSONObject jSONObject = new JSONObject(biz.obake.team.android.c.c(str2));
            jSONObject.put("title", str);
            biz.obake.team.android.c.e(jSONObject.toString(), str2);
            f.d().b();
        } catch (IOException | JSONException unused) {
        }
    }
}
